package com.lefevre.cms.service.question.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lefevre.cms.bean.question.QuestionIndex;
import com.lefevre.cms.mapper.QuestionIndexMapper;
import com.lefevre.cms.service.besa.DaoSupport;
import com.lefevre.cms.service.question.QuestionIndexService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 问题Lucene索引
 *
 * @author Lefevre
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class QuestionIndexServiceImpl extends DaoSupport<QuestionIndex> implements QuestionIndexService {

    @Resource
    private QuestionIndexMapper baseMapper;

    /**
     * 查询问题索引变化标记
     *
     * @param firstIndex
     * @param maxResult
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public List<QuestionIndex> findQuestionIndex(int firstIndex, int maxResult) {
        return baseMapper.selectPage(new Page<>(firstIndex, maxResult),
                new LambdaQueryWrapper<QuestionIndex>().orderByAsc(QuestionIndex::getId)).getRecords();
    }

    /**
     * 添加问题索引变化标记
     *
     * @param questionIndex 索引变化标记
     */
    public void addQuestionIndex(QuestionIndex questionIndex) {
        baseMapper.insert(questionIndex);
    }

    /**
     * 删除问题索引变化标记
     *
     * @param indexIdList 索引变化标记Id集合
     */
    public void deleteQuestionIndex(List<Long> indexIdList) {
        baseMapper.deleteBatchIds(indexIdList);
    }

    /**
     * 删除所有问题索引变化标记
     */
    public Integer deleteAllIndex() {
        return baseMapper.truncate();
    }

}
